<template>
  <div class="adduser-container">
    <el-form
      ref="formRef"
      :model="data.formData"
      label-width="80px"
      scroll-to-error
    >
      <el-form-item
        label="姓名"
        prop="name"
        :rules="[{ required: true, message: '请输入姓名' }]"
      >
        <el-input
          v-model="data.formData.name"
          placeholder="请输入姓名"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="账号"
        prop="account"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <el-input
          v-model="data.formData.account"
          placeholder="请输入账号"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="角色"
        prop="role"
        :rules="[{ required: true, message: '请选择角色' }]"
      >
        <el-select
          v-model="data.formData.role"
          placeholder="请选择角色"
          clearable
          :rules="[{ required: true, message: '请选择角色' }]"
        >
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="普通用户" value="普通用户"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="电话"
        prop="phone"
        :rules="[{ required: true, message: '请输入电话' }]"
      >
        <el-input
          v-model="data.formData.phone"
          placeholder="请输入电话"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="部门"
        prop="departmentId"
        :rules="[{ required: true, message: '请选择部门' }]"
      >
        <el-tree-select
          v-model="data.formData.departmentId"
          :data="data.departmentTrees"
          :props="{ label: 'name', value: 'id' }"
          default-expand-all
          check-strictly
          :render-after-expand="false"
          @change="events.handleDepartmentChange"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item
        label="职位"
        prop="jobId"
        :rules="[{ required: true, message: '请选择职位' }]"
      >
        <el-select
          v-model="data.formData.jobId"
          clearable
          placeholder="Select"
          style="width: 240px"
        >
          <el-option
            v-for="item in data.jobItems"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-popconfirm
        confirm-button-text="是"
        cancel-button-text="否"
        title="是否要重置表单?"
        @confirm="events.handleReset"
      >
        <template #reference>
          <el-button type="info" plain>重置</el-button>
        </template>
      </el-popconfirm>
      <el-button type="primary" @click="events.handleSubmit">添加</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { FormInstance, UploadInstance } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
import { getDeprtmentTree } from '../../../functions/department';
import { addUser } from '../../../functions/user';
import { toast } from '../../../utils/tools';
import { GetJobByDepartmentId } from '../../../functions/job';
import { useRouter } from 'vue-router';

interface Data {
  formData: any;
  departmentTrees: any[];
  jobItems: any[];
}

const data = reactive<Data>({
  formData: {
    id: 0,
    name: '',
    account: '',
    role: '',
    password: '',
    salt: '',
    phone: '',
    jobId: '',
    departmentId: '',
  },
  departmentTrees: [],
  jobItems: [],
});
const router = useRouter();
const formRef = ref(null);
const uploadRef = ref(null);

const events = {
  async handleDepartmentChange(value: any) {
    const res = await GetJobByDepartmentId(value);
    if (res.code >= 0) {
      data.jobItems = res.data;
      data.formData.jobId = '';
    }
  },
  handleReset() {
    const form = formRef.value as FormInstance | null;
    if (form) {
      form.resetFields();
      data.formData = {
        id: 0,
        name: '',
        account: '',
        role: '',
        password: '',
        salt: '',
        phone: '',
        jobId: '',
        isActive: false,
        isBan: false,
        departmentId: '',
      };
    }
  },
  handleSubmit() {
    const form = formRef.value as FormInstance | null;
    console.log(data.formData);
    if (form) {
      form
        .validate()
        .then(async (valid) => {
          console.log(valid);
          if (valid) {
            // 提交表单
            const res = await addUser(data.formData);
            if (res.code >= 0) {
              toast.success(res.message || '添加成功');
              router.push('/org/user');
            } else {
              toast.error(res.message || '添加失败');
            }
          }
        })
        .catch((e) => {});
    }
  },
};
onMounted(async () => {
  // 获取部门树
  const res = await getDeprtmentTree();
  if (res.code >= 0) {
    data.departmentTrees = res.data;
  }
});
</script>

<style scoped lang="scss">
.adduser-container {
  padding: 20px;
  padding-bottom: 20%;
}
.btn-container {
  margin-top: 20px;
  text-align: right;
}
</style>
